package com.hongdee.crm.group.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.group.entity.TCrmGroup;
import com.hongdee.crm.group.service.ITCrmGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/2/20.
 */
@RestController
@RequestMapping("/crm/group")
public class TCrmGroupController {
    @Autowired
    private ITCrmGroupService itCrmGroupService;
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc
     * @author 徐钦勇
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "type", defaultValue = "") String type) throws Exception {
        String oid= TokenUtil.getCompanyId(uid);
        Map map = itCrmGroupService.page(start, limit,oid,type);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc    启用start
     * @author 徐钦勇
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itCrmGroupService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return ResultEntity
     * @throws Exception
     * @desc   停用stop
     * @author 徐钦勇
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itCrmGroupService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc
     * @author 徐钦勇
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmGroup data) throws Exception {
        String oid= TokenUtil.getCompanyId(uid);
        data.setCompanyId(oid);
        TCrmGroup group = this.itCrmGroupService.saveOrUpdate(data);
        ResultEntity res = new ResultEntity();
        res.setData(group);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
    /**
         * @param  uid 登录用户id
         * @param  token 登录用户 token
         * @return Map
         * @throws Exception
         * @desc   分组明细
         * @author 徐钦勇
         */
        @RequestMapping("/info")
        @ResponseBody
        public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
            ResultEntity res = new ResultEntity();
            Map map = this.itCrmGroupService.info(id);
            res.setData(map);
            res.setSuccess(true);
            res.setMsg("查询详情成功");
            return res;
        }
    /**
     * @return Map
     * @throws Exception
     * @desc
     * @author 徐钦勇
     */
    @RequestMapping("/combo")
    @ResponseBody
    public List<Map> combo(@RequestParam(name = "type", defaultValue = "") String type) throws Exception {
        return this.itCrmGroupService.combo(type);
    }
    /**
     * @return Map
     * @throws Exception
     * @desc 分组编码转换
     * @author 徐钦勇
     */
    @RequestMapping("/convert")
    @ResponseBody
    public Map convert() throws Exception {
        return this.itCrmGroupService.convert();
    }
}
